package com.zt.utils;

import java.util.HashMap;
import java.util.Map;

import org.springframework.dao.CannotAcquireLockException;
import org.springframework.dao.CannotSerializeTransactionException;
import org.springframework.dao.CleanupFailureDataAccessException;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.dao.NonTransientDataAccessException;
import org.springframework.dao.NonTransientDataAccessResourceException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.dao.PermissionDeniedDataAccessException;
import org.springframework.dao.PessimisticLockingFailureException;
import org.springframework.dao.RecoverableDataAccessException;
import org.springframework.dao.TransientDataAccessException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.dao.TypeMismatchDataAccessException;
import org.springframework.dao.UncategorizedDataAccessException;
import org.springframework.jdbc.support.xml.SqlXmlFeatureNotImplementedException;

/**
 * DataExceptionProcess
 * 
 * @author zhangzhw
 * @describe 数据库异常处理
 */
public class DataExceptionProcess {

	/**
	 * 方法 processDBAException
	 * 
	 * @param e
	 * @describe 处理JDBC异常
	 */
	public static Map<String, Object> processDBAException(DataAccessException e) {
		Map<String, Object> errors = new HashMap<String, Object>();
		errors.put("title", "访问数据库异常");
		if (e instanceof CannotAcquireLockException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_CANNOTACQUIRELOCK);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof CannotSerializeTransactionException) {
			errors.put("errorMsg",
					DataExceptionConstants.DBE_CANNOTSERIALTRANSACTION);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof CleanupFailureDataAccessException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_CLOSERESOURCEFAIL);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof DataAccessResourceFailureException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_DATARESOURCEFAIL);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof DeadlockLoserDataAccessException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_DEADLOCK);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof DuplicateKeyException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_DUPLICATEKEY);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof EmptyResultDataAccessException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_EMPTYRESULT);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof IncorrectUpdateSemanticsDataAccessException) {
			errors.put("errorMsg",
					DataExceptionConstants.DBE_INCORRECTUPDATESEMAN);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof InvalidDataAccessResourceUsageException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_DATARESOURCE);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof OptimisticLockingFailureException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_LOCKINGFAIL);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof PermissionDeniedDataAccessException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_PERMISSIONDENIED);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof RecoverableDataAccessException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_RECOVERBAL);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof TransientDataAccessResourceException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_TRANSIENTRESOURCE);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof TypeMismatchDataAccessException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_MATCHDATAFAIL);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof SqlXmlFeatureNotImplementedException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_SQLXMLFEATURE);
			errors.put("detailMessage", e.getMessage());
		}
		// 有子类的异常要按层次
		else if (e instanceof PessimisticLockingFailureException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_PESSIMISTICLOCK);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof ConcurrencyFailureException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_CONCURRENCYFAIL);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof DataIntegrityViolationException) {
			errors.put("errorMsg",
					DataExceptionConstants.DBE_DATAINTEGRITYVIOLATION);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof IncorrectResultSizeDataAccessException) {
			errors.put("errorMsg",
					DataExceptionConstants.DBE_INCORRECTRESULTSIZE);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof DataRetrievalFailureException) {
			errors.put("errorMsg",
					DataExceptionConstants.DBE_INCORRECTRESULTSIZE);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof InvalidDataAccessApiUsageException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_INVALIDAPI);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof NonTransientDataAccessException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_NOTTRANSIENT);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof NonTransientDataAccessResourceException) {
			errors.put("errorMsg",
					DataExceptionConstants.DBE_NOTTRANSIENTRESOURCE);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof TransientDataAccessException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_TRANSIENT);
			errors.put("detailMessage", e.getMessage());
		} else if (e instanceof UncategorizedDataAccessException) {
			errors.put("errorMsg", DataExceptionConstants.DBE_OTHERS);
			errors.put("detailMessage", e.getMessage());
		}

		return errors;
	}

}
